<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['project:project:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['project:project:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['project:project:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['project:project:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table border v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="项目编码" align="center" prop="projectCode"/>
            <el-table-column label="项目名称" align="center" prop="projectName"/>
            <el-table-column label="项目全称" align="center" prop="projectFullName" :show-overflow-tooltip="true"/>
            <el-table-column label="项目类型" align="center" prop="projectType">
                <template #default="scope">
                    <dict-tag :options="pro_project_type" :value="scope.row.projectType"/>
                </template>
            </el-table-column>
            <el-table-column label="建设单位" align="center" prop="constructionUnit"/>
            <el-table-column label="总投资" align="center" prop="total"/>
            <el-table-column label="工期" align="center" prop="period"/>
            <el-table-column label="开工日期" align="center" prop="beginDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="完工日期" align="center" prop="endDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                  <div class="action-group">
                    <el-tooltip content="修改" placement="top">
                      <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                 v-hasPermi="['project:project:edit']"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                      <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                                 v-hasPermi="['project:project:remove']"></el-button>
                    </el-tooltip>
                    <!-- 更多操作下拉菜单 -->
                    <el-tooltip content="更多操作" placement="top">
                      <el-dropdown trigger="click" @command="(command) => handleCommand(command, scope.row)"
                                   v-hasPermi="['project:project:edit']">
                        <el-button link type="primary" icon="Operation" class="action-btn"></el-button>
                        <!-- 下拉菜单内容 -->
                        <template #dropdown>
                          <el-dropdown-menu>
                            <el-dropdown-item command="handleReport" icon="Key" divided>项目报表</el-dropdown-item>
                            <el-dropdown-item command="handleAuthRole" icon="CircleCheck" divided>中期支付证书
                            </el-dropdown-item>
                            <el-dropdown-item command="handleAuthMenu" icon="Menu" divided>监理中期支付证书</el-dropdown-item>
                          </el-dropdown-menu>
                        </template>
                      </el-dropdown>
                    </el-tooltip>
                  </div>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改建设项目对话框 -->
        <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
            <el-form ref="projectRef" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目编码" prop="projectCode">
                            <el-input v-model="form.projectCode" placeholder="请输入项目编码" maxlength="12" show-word-limit/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目名称" prop="projectName">
                            <el-input v-model="form.projectName" placeholder="请输入项目名称" maxlength="12" show-word-limit/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目全称" prop="projectFullName">
                            <el-input type="textarea" v-model="form.projectFullName" placeholder="请输入项目全称" maxlength="30" show-word-limit/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设单位" prop="constructionUnit">
                            <el-input type="textarea" v-model="form.constructionUnit" placeholder="请输入建设单位" maxlength="30" show-word-limit/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目类型" prop="projectType">
                            <el-select v-model="form.projectType" placeholder="请选择项目类型">
                                <el-option
                                        v-for="dict in pro_project_type"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属地区" prop="citys">
                            <el-cascader v-model="form.citys" placeholder="请选择所属地区" @change="handleSelectCity"
                                         :options="cityOptions" :props="{'value':'Name','label':'Name','children':'ChildList'}"
                                         style="width: 100%"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="总投资 " prop="total">
                            <el-input type="number" v-model="form.total" placeholder="请输入总投资金额">
                                <template #append>万元</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="工　　期" prop="period">
                            <el-input type="number" v-model="form.period" placeholder="请输入工期">
                                <template #append>单位（月）</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="开工日期" prop="beginDate">
                            <el-date-picker
                                v-model="form.beginDate"
                                align="right"
                                type="date"
                                placeholder="选择日期" style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="完工日期" prop="endDate">
                            <el-date-picker
                                v-model="form.endDate"
                                align="right"
                                type="date"
                                placeholder="选择日期" style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="项目概况" prop="describe">
                            <el-input v-model="form.describe" type="textarea" placeholder="请输入内容" maxlength="120" show-word-limit/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Project">
    import {list, getInfo, add, update, del} from "@/api/project/project";
    import cityJson from "@/json/city.json";

    const {proxy} = getCurrentInstance();
    const {pro_project_type} = proxy.useDict('pro_project_type');

    const projectList = ref([]);
    const open = ref(false);
    const loading = ref(true);
    const showSearch = ref(true);
    const ids = ref([]);
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);
    const title = ref("");

    const cityOptions = ref(cityJson);

    const data = reactive({
        form: {},
        queryParams: {
            pageNum: 1,
            pageSize: 10,
        },
        rules: {
            projectCode: [
                {required: true, message: "项目编码不能为空", trigger: "blur"}
            ],
            projectName: [
                {required: true, message: "项目名称不能为空", trigger: "blur"}
            ],
            projectFullName: [
              {required: true, message: "项目全称不能为空", trigger: "blur"}
            ],
        }
    });

    const {queryParams, form, rules} = toRefs(data);

    /** 查询建设项目列表 */
    function getList() {
        loading.value = true;
        list(queryParams.value).then(response => {
            projectList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }

    // 取消按钮
    function cancel() {
        open.value = false;
        reset();
    }

    // 表单重置
    function reset() {
        form.value = {
            projectId: null,
            platformCode: null,
            projectCode: null,
            projectFullName: null,
            projectName: null,
            projectType: null,
            constructionUnit: null,
            total: null,
            period: null,
            beginDate: null,
            endDate: null,
            describe: null,
            createBy: null,
            createTime: null,
            updateBy: null,
            updateTime: null
        };
        proxy.resetForm("projectRef");
    }

    /** 搜索按钮操作 */
    function handleQuery() {
        queryParams.value.pageNum = 1;
        getList();
    }

    /** 重置按钮操作 */
    function resetQuery() {
        proxy.resetForm("queryRef");
        handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.projectId);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    function handleSelectCity(value) {
        console.log(value);
    }

    /** 新增按钮操作 */
    function handleAdd() {
        reset();
        open.value = true;
        title.value = "添加建设项目";
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
        reset();
        const _projectId = row.projectId || ids.value
        getInfo(_projectId).then(response => {
            form.value = response.data;
            open.value = true;
            title.value = "修改建设项目";
        });
    }

    /** 提交按钮 */
    function submitForm() {
        proxy.$refs["projectRef"].validate(valid => {
            if (valid) {
                if (form.value.projectId != null) {
                    update(form.value).then(response => {
                        proxy.$modal.msgSuccess("修改成功");
                        open.value = false;
                        getList();
                    });
                } else {
                    add(form.value).then(response => {
                        proxy.$modal.msgSuccess("新增成功");
                        open.value = false;
                        getList();
                    });
                }
            }
        });
    }

    /** 删除按钮操作 */
    function handleDelete(row) {
        const _projectIds = row.projectId || ids.value;
        proxy.$modal.confirm('是否确认删除建设项目编号为"' + _projectIds + '"的数据项？').then(function () {
            return del(_projectIds);
        }).then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
    }

    /** 导出按钮操作 */
    function handleExport() {
        proxy.download('project/project/export', {
            ...queryParams.value
        }, `project_${new Date().getTime()}.xlsx`)
    }

    /** 更多操作 */
    function handleCommand(command, row) {
      switch (command) {
        case "handleReport":
          handleReport(row);
          break;
        case "handleReport":
          handleReport(row);
          break;
        case "handleReport":
          handleReport(row);
          break;
        default:
          break;
      }
    };

    /** 跳转项目报表设置页面 */
    function handleReport(row) {
      const userId = row.userId;
      router.push("/system/user-auth/role/" + userId);
    };

    getList();
</script>

<style lang='scss' scoped>
/* 关键样式修正 */
.action-group {
  display: inline-flex;
  align-items: center;
  gap: 8px; /* 统一间隔 */
  position: relative;
  top: 1px; /* 微调垂直偏移 */
}

/* 深度选择器穿透组件样式 */
:deep(.action-btn) {
  --button-padding: 8px 12px; /* 自定义统一内边距 */

  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  padding: var(--button-padding) !important;
  /*min-width: 60px; !* 最小宽度统一 *!*/

  /* 精确字号和行高 */
  font-size: 14px;
  line-height: 1.2857; /* 18px / 14px */

  /* 消除默认边距 */
  margin: 0 !important;
}

/* 图标精准对齐 */
:deep(.el-dropdown .el-icon) {
  margin-left: 4px;
  font-size: 14px;
  position: relative;
  top: 0.5px; /* 微调图标垂直位置 */
}

</style>